package cc.uncarbon.module.appapi.service;


import cc.uncarbon.framework.core.context.UserContextHolder;
import cc.uncarbon.framework.core.exception.BusinessException;
import cc.uncarbon.module.appapi.mapper.OrdersItemMapper;
import cc.uncarbon.module.appapi.mapper.OrdersMapper;
import cc.uncarbon.module.appapi.model.request.OrdersItemDTO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yanrumiao.entity.OrdersEntity;
import com.yanrumiao.entity.OrdersItemEntity;
import com.yanrumiao.enumns.OrdersStatusEnum;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.prefs.BackingStoreException;

import static cc.uncarbon.module.appapi.util.OrderNumberGenerator.getDateFromString;

@RequiredArgsConstructor
@Service
@Slf4j
public class OrdersItemService {
    public final OrdersItemMapper ordersItemMapper;
    public final OrdersMapper ordersMapper;

    @Transactional(rollbackFor = Exception.class)
    public Long createOrder(OrdersItemDTO dto) {
        OrdersItemEntity entity = ordersItemMapper.selectOne(new LambdaQueryWrapper<OrdersItemEntity>()
                .eq(OrdersItemEntity::getStaffId, dto.getStaffId())
                .eq(OrdersItemEntity::getDate, getDateFromString(dto.getDate()))
                .eq(OrdersItemEntity::getTime, dto.getTime()));
        if (entity == null) {
            OrdersItemEntity ordersItem = new OrdersItemEntity();
            ordersItem.setOrderId(dto.getOrderId());
            ordersItem.setStaffId(dto.getStaffId());
            ordersItem.setDate(getDateFromString(dto.getDate()));
            ordersItem.setTime(dto.getTime());
            ordersItem.setStatus(OrdersStatusEnum.ORDER_DYY);
            ordersItem.setUserId(UserContextHolder.getUserId());
            ordersItem.setStoreId(dto.getStoreId());
            ordersItemMapper.insert(ordersItem);
            //修改订单状态
            OrdersEntity orders = ordersMapper.selectById(dto.getOrderId());
            if (orders != null) {
                orders.setStatus(OrdersStatusEnum.ORDER_DYY);
                ordersMapper.updateById(orders);
            }else{
                throw   new BusinessException(500,"订单不存在");
            }

            return ordersItem.getId();
        }
        return null;
    }

    public Object updateOrderStatus(Long id) {
        OrdersItemEntity ordersItemEntity = ordersItemMapper.selectById(id);
        if(ordersItemEntity != null){
            ordersItemEntity.setStatus(OrdersStatusEnum.ORDER_DFW);
            ordersItemMapper.updateById(ordersItemEntity);

            OrdersEntity orders = ordersMapper.selectById(ordersItemEntity.getOrderId());
            if(orders != null){
                orders.setStatus(OrdersStatusEnum.ORDER_DFW);
                ordersMapper.updateById(orders);
                return ordersItemEntity.getId();
            }
        }
        return null;

    }
}
